using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework.Graphics;

namespace ScytheEngine
{
    public class SEModel : ScytheEngine.Object,
        ILoadable,
        IRenderableChildren
    {
        #region Members

        private string m_Asset;
        private Model  m_Model;

        #endregion

        #region Ctors

        public SEModel(string asset)
        {
            m_Asset = asset;
        }

        #endregion

        #region Methods
        #endregion

        #region ILoadable Members

        public void LoadContent(GraphicsDevice gfxDev, Microsoft.Xna.Framework.Content.ContentManager loader)
        {
            m_Model = loader.Load<Model>(m_Asset);
        }

        #endregion

        #region IRenderableChildren Members

        public void RenderChildren(GraphicsDevice gfxDev)
        {
            Shader shader = ShaderManager.GetShader(m_Shader);
            shader.SetParameters(this);

            foreach(ModelMesh mesh in m_Model.Meshes)
            {
                gfxDev.Indices = mesh.IndexBuffer;
                shader.Effect.Begin();

                foreach(EffectPass pass in shader.Effect.CurrentTechnique.Passes)
                {
                    pass.Begin();

                    foreach(ModelMeshPart part in mesh.MeshParts)
                    {
                        gfxDev.VertexDeclaration = part.VertexDeclaration;
                        gfxDev.Vertices[0].SetSource(
                            mesh.VertexBuffer,
                            part.StreamOffset,
                            part.VertexStride);

                        gfxDev.Textures[0] = ((BasicEffect)part.Effect).Texture;

                        gfxDev.DrawIndexedPrimitives(
                            PrimitiveType.TriangleList,
                            part.BaseVertex,
                            0,
                            part.NumVertices,
                            part.StartIndex,
                            part.PrimitiveCount);
                    }

                    pass.End();
                }

                shader.Effect.End();
            }
        }

        #endregion
    }
}
